package chen.web.forum;

import chen.web.MyContextProvider;
import chen.web.forum.Subject.TopRank;
import chen.web.user.PermissionConfigRepository;

public class TopicPermission {
	
	private CategoryManager cm = null;
	
	public TopicPermission(CategoryManager cm){
		this.cm = cm;
	}

	public boolean cancelTop(Subject subject){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isCancelTop() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean delete(Subject subject){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isDeleteSubject() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean delete(Reply reply){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isDeleteReply() && inManagementCategorys(reply.getBelongSubject().getCategory());
	}
	
	public boolean forbidReply(Subject subject, boolean forbid){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isForbidReply() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean hideReplyContent(Reply reply, boolean hide){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isHideReplyContent() && inManagementCategorys(reply.getBelongSubject().getCategory());
	}
	
	public boolean hideSubjectContent(Subject subject, boolean hide){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isHideSubjectContent() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean post(Subject subject){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isPostSubject();
	}
	
	public boolean post(Reply reply){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isPostReply();
	}
	
	public boolean setDigest(Subject subject, boolean digest){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isSetDigest() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean setTop(Subject subject, TopRank rank){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isSetTop() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean update(Subject subject){
		if(PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isUpdateSubject()){
			return true;
		}
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isUpdateOwnSubject() && MyContextProvider.getMyContext().getAccount().equals(subject.getAuthor());
	}
	
	public boolean update(Reply reply){
		if(PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isUpdateReply()){
			return true;
		}
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isUpdateOwnReply() && MyContextProvider.getMyContext().getAccount().equals(reply.getAuthor());
	}
	
	public boolean move(Subject subject){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isMoveSubject() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean checked(Subject subject){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isCheckSubject() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean checked(Reply reply){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isCheckReply() && inManagementCategorys(reply.getBelongSubject().getCategory());
	}
		
	public boolean restore(Subject subject){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isRestoreSubject() && inManagementCategorys(subject.getCategory());
	}
	
	public boolean restore(Reply reply){
		return PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).isRestoreReply() && inManagementCategorys(reply.getBelongSubject().getCategory());
	}
	
	public boolean inManagementCategorys(Category category){
		for(int id : PermissionConfigRepository.loadCurrent(TopicPermissionConfig.class).getManagementCategoryIds()){
			Category cat = cm.get(id);
			if(cat != null && Category.getFromCategory(cat, category.getId()) != null){
				return true;
			}
		}		
		return false;
	}
	
	
}
